package com.hlc.helloWord.user.service.impl;

import com.hlc.helloWord.common.exception.MyException;
import com.hlc.helloWord.common.md5.MD5Util;
import com.hlc.helloWord.common.redis.RedisUtil;
import com.hlc.helloWord.common.result.ResultEnum;
import com.hlc.helloWord.common.token.TokenUtil;
import com.hlc.helloWord.user.dao.UserDao;
import com.hlc.helloWord.user.entity.User;
import com.hlc.helloWord.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * Created by helinchong on 2017/12/20.
 */
@Service
public class UserSerivceImpl implements UserService {

    @Autowired
    private UserDao userDao;

    @Autowired
    private RedisUtil redisUtil;

    @Autowired
    private TokenUtil tokenUtil;

    public User getUserInfoById(User user) {
        return userDao.getUserInfoById(user);
    }

    public User signIn(User user) {
        User loginUser = userDao.signIn(user);
        //判断此用户是否存在
        if(loginUser!=null){
            //判断密码是否输入正确
            if(loginUser.getPassword().equals(MD5Util.md5(user.getPassword()))){
                //生成token 并保存到redis
                tokenUtil.setToken(loginUser);
                return loginUser;
            }else{
                throw new MyException(ResultEnum.RELOGIN.getCode(),"密码输入有误!");
            }
        }else{
            throw new MyException(ResultEnum.RELOGIN.getCode(),"该用户未注册!");
        }
    }

}
